package org.gap.jseed.apf;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import org.gap.jseed.contract.annotation.NotEmpty;
import org.gap.jseed.contract.annotation.NotNull;

@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class ContractProcessor extends AbstractProcessor {
	private static final List<AnnotatedMethodProcessor> processors = new ArrayList<AnnotatedMethodProcessor>();
	{
		processors.add(new NotNullProcessor());
		processors.add(new NotEmptyProcessor());
	}
	
	@Override
	public Set<String> getSupportedAnnotationTypes() {
		return new HashSet<String>(Arrays.asList(NotNull.class.getName(), NotEmpty.class.getName()));
	}
	
	@Override
	public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
		MessageWriter messageWriter = new MessageWriter(processingEnv);
		for (AnnotatedMethodProcessor each : processors) {
			Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(each.getSupportedAnnotation());
			each.process(elements, messageWriter);
		}
		return true;
	}

}
